import requests
from typing import Optional

class NovelLLM:
    def __init__(self):
        self.endpoint = 'https://open.bigmodel.cn/api/paas/v4/chat/completions'
        self.key = '734c6ee4a5524052a1fc44580660dc38.F5A02rjHWxocAsaT'
        self.model = 'glm-4-flash'

    def call_llm(self, prompt: str, caller_name: Optional[str] = None) -> str:
        headers = {
            'Authorization': f'Bearer {self.key}',
            'Content-Type': 'application/json'
        }
        
        payload = {
            "model": self.model,
            "messages": [{"role": "user", "content": prompt}]
        }

        print(f"[{caller_name}] 调用LLM:\n输入：{prompt[:200]}...\n{'-'*30}")
        
        response = requests.post(self.endpoint, json=payload, headers=headers)
        response.raise_for_status()
        
        result = response.json()['choices'][0]['message']['content']
        print(f"[{caller_name}] 收到响应:\n{result[:200]}...\n{'-'*30}")
        
        return result